package pl.edu.agh.uddiProxy.types;

import java.util.Map;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.dao.DynamicParameterDAO;

public class ParametersMap {
	private Map<String, ParameterType> parametersMap;

	public Map<String, ParameterType> getParametersMap() {
		return parametersMap;
	}

	public void setParametersMap(Map<String, ParameterType> parametersMap) {
		this.parametersMap = parametersMap;
	}
	
	public void update(String tModelKey) {
		DynamicParameterDAO parameterDAO = DAOHelper.getDynamicParameterDAO();
		
		for (String name : parametersMap.keySet()) {
			ParameterType type = parametersMap.get(name);
			
			if (type.getCollector() == null) {
				continue;
			}
			
			Object param = type.getCollector().getParameter(DAOHelper.getUDDIClient().getInquiryClient().getTModel(tModelKey));
			
			ServiceDynamicParameters dynamicParameters;
			if (parameterDAO.exists(tModelKey)) {
				dynamicParameters = parameterDAO.getByTModel(tModelKey);
			} else {
				dynamicParameters = new ServiceDynamicParameters(tModelKey);
			}
			
			ParameterValue value = dynamicParameters.getParameterValue(name);
			value.incrementNumberOfMeasurements();
			type.getCounter().count(type, value, param);
			
			if (DAOHelper.getDynamicParameterDAO().exists(tModelKey)) {
				DAOHelper.getDynamicParameterDAO().update(dynamicParameters);
			} else {
				DAOHelper.getDynamicParameterDAO().create(dynamicParameters);	
			}
			
		}
	}

}
